﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SkypeStudio.Controls.ViewModels;
using SkypeStudio.Controls.Views;
using SkypeStudio.Controls.Test.ViewMock;
using SkypeStudio.Controls.Services;
using Rhino.Mocks;
using Rhino.Mocks.Interfaces;
using SkypeStudio.Controls.Model;

namespace SkypeStudio.Controls.Test.ViewModel
{
    [TestClass]
    public class SendMessageViewModelTest : BaseTest
    {
        [TestMethod]
        public void SkypeEventListeningTest()
        {
            ISkypeApplicationService skypeService = _Mock.StrictMock<ISkypeApplicationService>();
            ISendMessageView view = new MockSendMessageView();
            IEventRaiser receiveMessage = null;
            IEventRaiser sendingMessage = null;
            ChatMessage expectReceivedMessage = new ChatMessage() { User = new Contact { Handle = "A" }, Message = "GO GO" };
            ChatMessage expectSendingMessage = new ChatMessage() { User = new Contact { Handle = "B" }, Message = "GO GO A" };

            using (_Mock.Record())
            {
                receiveMessage = Expect.Call(delegate { skypeService.MessageReceived += null; }).IgnoreArguments().GetEventRaiser();
                sendingMessage = Expect.Call(delegate { skypeService.MessageSending += null; }).IgnoreArguments().GetEventRaiser();
            }

            using (_Mock.Playback())
            {
                SendMessageViewModel target = new SendMessageViewModel(view, skypeService);
                receiveMessage.Raise(expectReceivedMessage);

                Assert.AreEqual(1, target.ChatMessages.Count);
                Assert.AreEqual(expectReceivedMessage.User, target.ChatMessages[0].User);
                Assert.AreEqual(expectReceivedMessage.Message, target.ChatMessages[0].Message);

                sendingMessage.Raise(expectSendingMessage);

                Assert.AreEqual(2, target.ChatMessages.Count);
                Assert.AreEqual(expectSendingMessage.User, target.ChatMessages[1].User);
                Assert.AreEqual(expectSendingMessage.Message, target.ChatMessages[1].Message);
            }
        }

        [TestMethod]
        public void SendCommandTest()
        {
            ISkypeApplicationService skypeService = _Mock.StrictMock<ISkypeApplicationService>();
            ISendMessageView view = new MockSendMessageView();
            IEventRaiser receiveMessage = null;
            IEventRaiser sendingMessage = null;
            ChatMessage expectChatMessge = new ChatMessage() { User = new Contact { Handle = "A" }, Message = "GO GO" };
            Contact f1 = new Contact { Handle = "B" };
            string testMessage = "message";

            Chat chat = new Chat() { Members = new System.Collections.ObjectModel.ObservableCollection<Contact>() { f1 },  ChatName ="name" };

            using (_Mock.Record())
            {
                receiveMessage = Expect.Call(delegate { skypeService.MessageReceived += null; }).IgnoreArguments().GetEventRaiser();
                sendingMessage = Expect.Call(delegate { skypeService.MessageSending += null; }).IgnoreArguments().GetEventRaiser();
                Expect.Call(delegate { skypeService.SendMessage(f1.Handle, testMessage); });
            }

            using (_Mock.Playback())
            {
                SendMessageViewModel target = new SendMessageViewModel(view, skypeService);
                target.ChatSource = chat;
                target.SendText = testMessage;

                target.SendMessageCommand.Execute(null);

                Assert.AreEqual(string.Empty, target.SendText);
            }
        }
    }
}
